package com.example.wlt.personnel_manegement_system.Adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.wlt.personnel_manegement_system.Item.Item_managemember;
import com.example.wlt.personnel_manegement_system.R;

import java.util.List;

/**
 * Created by WLT on 2016/5/18.
 */
public class MemberAdapter extends BaseAdapter {
    private List<Item_managemember> mList;
    private LayoutInflater mInflater;
    private Activity activity;
    public MemberAdapter(Activity activity,Context context, List<Item_managemember> List){
        mList=List;
        mInflater=LayoutInflater.from(context);
        this.activity=activity;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      ViewHolder holder=null;
        if(convertView==null) {
            convertView = mInflater.inflate(R.layout.person_memmanage_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_memmanage_name);
            holder.department = (TextView) convertView.findViewById(R.id.tv_memmanage_department);
            holder.clear = (TextView) convertView.findViewById(R.id.tv_memmanage_clear);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        Item_managemember mItem=mList.get(position);
        holder.name.setText(mItem.Itemname);
        holder.department.setText(mItem.Itemdepartment);
        holder.clear.setText(mItem.Itemclear);
        final ViewHolder finalHolder = holder;
        holder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(position);
            }
        });
        return convertView;
    }
    private void showDialog(final int position){
        AlertDialog.Builder builder=new AlertDialog.Builder(activity);
        builder.setMessage("您将清退该同学");
        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });
        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }
    class ViewHolder{
        protected TextView name;
        protected TextView department;
        protected TextView clear;
    }

}
